using Microsoft.EntityFrameworkCore;
using System;
using Terra.Data.Entities.Identity;

namespace Terra.Data.SeedData.IdentitySeedData;

/// <summary>
/// 部门种子数据提供者
/// </summary>
public class DeptSeedData : ISeedDataProvider
{
    /// <summary>
    /// 系统内置部门ID常量
    /// </summary>
    public static class SystemDeptIds
    {
        /// <summary>
        /// 总公司
        /// </summary>
        public static readonly Guid HeadquartersId = Guid.Parse("A0DA03D1-D316-4C15-AD3F-F3D8AEB2472F");

        /// <summary>
        /// 技术中心
        /// </summary>
        public static readonly Guid TechnologyCenterId = Guid.Parse("B1CFACB2-F0EE-4A6E-B6C6-45C91D35ACE9");

        /// <summary>
        /// 营销中心
        /// </summary>
        public static readonly Guid MarketingCenterId = Guid.Parse("C33894EE-A40A-4D42-8130-8F7305D2A10D");

        /// <summary>
        /// 人力资源部
        /// </summary>
        public static readonly Guid HrDeptId = Guid.Parse("D6F09DEF-BC69-4472-801D-7F6684F65BD0");

        /// <summary>
        /// 财务部
        /// </summary>
        public static readonly Guid FinanceDeptId = Guid.Parse("E5B24114-DBE6-4EB2-9D3D-B4143929BB9F");
    }

    /// <summary>
    /// 配置部门种子数据
    /// </summary>
    /// <param name="modelBuilder">模型构建器</param>
    public void Configure(ModelBuilder modelBuilder)
    {
        var now = DateTime.UtcNow;
        var adminId = UserSeedData.SystemUserIds.AdminUserId;

        #region 公司总部

        // 1. 总公司
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = SystemDeptIds.HeadquartersId,
            Name = "总公司",
            Code = "HQ",
            FullName = "Terra科技有限公司",
            Sort = 10,
            Status = 1,
            Level = 1,
            Path = $"/{SystemDeptIds.HeadquartersId}/",
            Remark = "公司总部",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        #endregion

        #region 一级部门

        // 2. 技术中心
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = SystemDeptIds.TechnologyCenterId,
            ParentId = SystemDeptIds.HeadquartersId,
            Name = "技术中心",
            Code = "TC",
            FullName = "技术研发中心",
            Sort = 20,
            Status = 1,
            Level = 2,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.TechnologyCenterId}/",
            Remark = "负责产品研发和技术支持",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        // 3. 营销中心
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = SystemDeptIds.MarketingCenterId,
            ParentId = SystemDeptIds.HeadquartersId,
            Name = "营销中心",
            Code = "MC",
            FullName = "市场营销中心",
            Sort = 30,
            Status = 1,
            Level = 2,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.MarketingCenterId}/",
            Remark = "负责市场营销和客户拓展",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        // 4. 人力资源部
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = SystemDeptIds.HrDeptId,
            ParentId = SystemDeptIds.HeadquartersId,
            Name = "人力资源部",
            Code = "HR",
            FullName = "人力资源管理部",
            Sort = 40,
            Status = 1,
            Level = 2,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.HrDeptId}/",
            Remark = "负责人才招聘和培训发展",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        // 5. 财务部
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = SystemDeptIds.FinanceDeptId,
            ParentId = SystemDeptIds.HeadquartersId,
            Name = "财务部",
            Code = "FD",
            FullName = "财务管理部",
            Sort = 50,
            Status = 1,
            Level = 2,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.FinanceDeptId}/",
            Remark = "负责财务管理和成本控制",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        #endregion

        #region 技术中心二级部门

        // 6. 研发部
        var devDeptId = Guid.Parse("F27876B2-D2EE-4782-8421-87CCD90E1B7C");
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = devDeptId,
            ParentId = SystemDeptIds.TechnologyCenterId,
            Name = "研发部",
            Code = "DEV",
            FullName = "研发部",
            Sort = 21,
            Status = 1,
            Level = 3,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.TechnologyCenterId}/{devDeptId}/",
            Remark = "负责产品开发",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        // 7. 测试部
        var qaDeptId = Guid.Parse("A7F6D15F-504C-41C6-ABAE-B83D325140F8");
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = qaDeptId,
            ParentId = SystemDeptIds.TechnologyCenterId,
            Name = "测试部",
            Code = "QA",
            FullName = "质量保障部",
            Sort = 22,
            Status = 1,
            Level = 3,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.TechnologyCenterId}/{qaDeptId}/",
            Remark = "负责质量保障",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        // 8. 运维部
        var opsDeptId = Guid.Parse("B06B4C1D-339B-4D20-BEA8-37C13FDF98F3");
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = opsDeptId,
            ParentId = SystemDeptIds.TechnologyCenterId,
            Name = "运维部",
            Code = "OPS",
            FullName = "运维管理部",
            Sort = 23,
            Status = 1,
            Level = 3,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.TechnologyCenterId}/{opsDeptId}/",
            Remark = "负责系统运维",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        // 9. 产品部
        var pmDeptId = Guid.Parse("C1A6E125-DD60-4604-9E7E-7DF25C0CB5CC");
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = pmDeptId,
            ParentId = SystemDeptIds.TechnologyCenterId,
            Name = "产品部",
            Code = "PM",
            FullName = "产品管理部",
            Sort = 24,
            Status = 1,
            Level = 3,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.TechnologyCenterId}/{pmDeptId}/",
            Remark = "负责产品设计与规划",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        #endregion

        #region 营销中心二级部门

        // 10. 销售部
        var salesDeptId = Guid.Parse("D80E8265-B970-4789-BEB1-DA1C4B4ECE83");
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = salesDeptId,
            ParentId = SystemDeptIds.MarketingCenterId,
            Name = "销售部",
            Code = "SALES",
            FullName = "销售部",
            Sort = 31,
            Status = 1,
            Level = 3,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.MarketingCenterId}/{salesDeptId}/",
            Remark = "负责产品销售",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        // 11. 市场部
        var marketDeptId = Guid.Parse("E68FC23A-3F87-46AC-9BFE-C1F7A2F6C073");
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = marketDeptId,
            ParentId = SystemDeptIds.MarketingCenterId,
            Name = "市场部",
            Code = "MKT",
            FullName = "市场推广部",
            Sort = 32,
            Status = 1,
            Level = 3,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.MarketingCenterId}/{marketDeptId}/",
            Remark = "负责市场推广",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        // 12. 客服部
        var csDeptId = Guid.Parse("F0DDB3B0-C15D-4C65-B0A9-5617A08F86A2");
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = csDeptId,
            ParentId = SystemDeptIds.MarketingCenterId,
            Name = "客服部",
            Code = "CS",
            FullName = "客户服务部",
            Sort = 33,
            Status = 1,
            Level = 3,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.MarketingCenterId}/{csDeptId}/",
            Remark = "负责客户服务与支持",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        #endregion

        #region 人力资源部二级部门

        // 13. 招聘部
        var recruitDeptId = Guid.Parse("A5F6D150-D0DD-43C6-ABDF-BF3D321D40F9");
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = recruitDeptId,
            ParentId = SystemDeptIds.HrDeptId,
            Name = "招聘部",
            Code = "REC",
            FullName = "人才招聘部",
            Sort = 41,
            Status = 1,
            Level = 3,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.HrDeptId}/{recruitDeptId}/",
            Remark = "负责人才招聘",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        // 14. 培训部
        var trainingDeptId = Guid.Parse("B6B6C12D-3A9C-4D21-BFA9-C7C23FD988F4");
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = trainingDeptId,
            ParentId = SystemDeptIds.HrDeptId,
            Name = "培训部",
            Code = "TRN",
            FullName = "员工培训发展部",
            Sort = 42,
            Status = 1,
            Level = 3,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.HrDeptId}/{trainingDeptId}/",
            Remark = "负责员工培训与发展",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        // 15. 行政部
        var adminDeptId = Guid.Parse("C74462E0-FC48-4F9F-B0E9-2C90B1D58323");
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = adminDeptId,
            ParentId = SystemDeptIds.HrDeptId,
            Name = "行政部",
            Code = "ADM",
            FullName = "行政管理部",
            Sort = 43,
            Status = 1,
            Level = 3,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.HrDeptId}/{adminDeptId}/",
            Remark = "负责日常行政事务",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        #endregion

        #region 财务部二级部门

        // 16. 会计部
        var accountingDeptId = Guid.Parse("D9E7A3B5-E011-4A1F-8C94-C30A02493113");
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = accountingDeptId,
            ParentId = SystemDeptIds.FinanceDeptId,
            Name = "会计部",
            Code = "ACC",
            FullName = "会计核算部",
            Sort = 51,
            Status = 1,
            Level = 3,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.FinanceDeptId}/{accountingDeptId}/",
            Remark = "负责财务核算",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        // 17. 资金部
        var treasuryDeptId = Guid.Parse("E0E29C44-5890-417B-9CAB-D41EAEE03DCB");
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = treasuryDeptId,
            ParentId = SystemDeptIds.FinanceDeptId,
            Name = "资金部",
            Code = "TRE",
            FullName = "资金管理部",
            Sort = 52,
            Status = 1,
            Level = 3,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.FinanceDeptId}/{treasuryDeptId}/",
            Remark = "负责资金调配和管理",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        // 18. 税务部
        var taxDeptId = Guid.Parse("F1DFD512-F2C2-487F-A1CA-E5EC6CBFDECE");
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = taxDeptId,
            ParentId = SystemDeptIds.FinanceDeptId,
            Name = "税务部",
            Code = "TAX",
            FullName = "税务管理部",
            Sort = 53,
            Status = 1,
            Level = 3,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.FinanceDeptId}/{taxDeptId}/",
            Remark = "负责税务规划与管理",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        #endregion

        #region 研发部三级部门

        // 19. 前端组
        var frontendTeamId = Guid.Parse("A2C47A13-317B-4E16-B629-25DE7AC0899B");
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = frontendTeamId,
            ParentId = devDeptId,
            Name = "前端组",
            Code = "FE",
            FullName = "前端开发组",
            Sort = 211,
            Status = 1,
            Level = 4,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.TechnologyCenterId}/{devDeptId}/{frontendTeamId}/",
            Remark = "负责前端开发",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        // 20. 后端组
        var backendTeamId = Guid.Parse("B3C58DB7-42B9-42D8-A70A-3C07FA2F3AED");
        modelBuilder.Entity<Dept>().HasData(new Dept
        {
            Id = backendTeamId,
            ParentId = devDeptId,
            Name = "后端组",
            Code = "BE",
            FullName = "后端开发组",
            Sort = 212,
            Status = 1,
            Level = 4,
            Path = $"/{SystemDeptIds.HeadquartersId}/{SystemDeptIds.TechnologyCenterId}/{devDeptId}/{backendTeamId}/",
            Remark = "负责后端开发",
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        #endregion
    }

    /// <summary>
    /// 获取提供者优先级（在角色种子数据之后）
    /// </summary>
    public int Order => 40;
}